﻿Imports System.Data
Imports System.Data.SqlClient

Public Class Materiales

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Main.WindowState = FormWindowState.Normal
        Me.Close()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Main.WindowState = FormWindowState.Normal
        Me.Close()
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Main.WindowState = FormWindowState.Normal
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim strCon As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql As String = "select * from proveedor where rut_prov='" & TextBox13.Text & "-" & TextBox12.Text & "';"
            Dim tabla As New DataTable
            Dim adaptador As New SqlDataAdapter(sql, strCon)
            adaptador.Fill(tabla)

            Dim strCon2 As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql2 As String = "select * from proveedor where rut_prov='" & TextBox13.Text & "-" & TextBox12.Text & "';"
            Dim tabla2 As New DataTable
            Dim adaptador2 As New SqlDataAdapter(sql2, strCon2)
            adaptador2.Fill(tabla2)

            DataGridView1.DataSource = tabla2


            TextBox6.Text = tabla.Rows(0)("nombre")
            TextBox9.Text = tabla.Rows(0)("telefono")
            TextBox10.Text = tabla.Rows(0)("direccion")

            TextBox4.Enabled = False
            TextBox5.Enabled = False
            TextBox6.Enabled = False
            TextBox9.Enabled = False
            TextBox10.Enabled = False

        Catch ex As Exception
            MsgBox("Usuario no registrado", MsgBoxStyle.Critical, "Error")

        End Try
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox7.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox9.Text = ""
        TextBox10.Text = ""

        TextBox4.Enabled = True
        TextBox5.Enabled = True
        TextBox6.Enabled = True
        TextBox9.Enabled = True
        TextBox10.Enabled = True


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim strCon As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql As String = "insert into proveedor values ('" & TextBox4.Text & "-" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "');"
            Dim tabla As New DataTable
            Dim adaptador As New SqlDataAdapter(sql, strCon)
            adaptador.Fill(tabla)

            Dim strCon2 As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql2 As String = "insert into materia_prima values ('" & TextBox2.Text & "','" & TextBox1.Text & "','" & TextBox3.Text & "','" & TextBox7.Text & "','" & TextBox4.Text & "-" & TextBox5.Text & "');"
            Dim tabla2 As New DataTable
            Dim adaptador2 As New SqlDataAdapter(sql2, strCon2)
            adaptador2.Fill(tabla2)


        Catch ex As Exception
            MsgBox("Usuario no registrado", MsgBoxStyle.Critical, "Error")

        End Try
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Try
            
            Dim strCon2 As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql2 As String = "insert into materia_prima values ('" & TextBox2.Text & "','" & TextBox1.Text & "','" & TextBox3.Text & "','" & TextBox7.Text & "','" & TextBox4.Text & "-" & TextBox5.Text & "');"
            Dim tabla2 As New DataTable
            Dim adaptador2 As New SqlDataAdapter(sql2, strCon2)
            adaptador2.Fill(tabla2)


        Catch ex As Exception
            MsgBox("Usuario no registrado", MsgBoxStyle.Critical, "Error")

        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox16.Text = ""
        TextBox15.Text = ""
        TextBox14.Text = ""
        TextBox17.Text = ""
        TextBox8.Text = ""

        TextBox18.Text = ""
        TextBox13.Text = ""
        TextBox12.Text = ""
        TextBox11.Text = ""
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Try
            Dim strCon As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql As String = "insert into activo_fijo values ('" & TextBox16.Text & "','" & TextBox8.Text & "','" & TextBox15.Text & "','" & TextBox17.Text & "','" & TextBox17.Text & "','" & TextBox20.Text & "-" & TextBox14.Text & "');"
            Dim tabla As New DataTable
            Dim adaptador As New SqlDataAdapter(sql, strCon)
            adaptador.Fill(tabla)

            Dim strCon2 As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql2 As String = "insert into materia_prima values ('" & TextBox18.Text & "','" & TextBox11.Text & "','" & TextBox13.Text & "','" & TextBox12.Text & "','" & TextBox20.Text & "-" & TextBox14.Text & "');"
            Dim tabla2 As New DataTable
            Dim adaptador2 As New SqlDataAdapter(sql2, strCon2)
            adaptador2.Fill(tabla2)

            TextBox16.Text = ""
            TextBox15.Text = ""
            TextBox14.Text = ""
            TextBox17.Text = ""
            TextBox8.Text = ""

            TextBox18.Text = ""
            TextBox13.Text = ""
            TextBox12.Text = ""
            TextBox11.Text = ""

            MsgBox("Datos guardados con exito", MsgBoxStyle.Information, "OK")
        Catch ex As Exception
            MsgBox("Usuario no registrado", MsgBoxStyle.Critical, "Error")

        End Try
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Label29.Text = "**********************"

        Label31.Text = "**********************"
        Label32.Text = "**********************"
        Label33.Text = "**********************"
        ComboBox1.Text = ""
        ComboBox3.Text = ""
        TextBox19.Text = ""
        TextBox21.Text = ""
        RichTextBox1.Text = ""
    End Sub



    Private Sub Materiales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim strCon As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql As String = "select id_af from activo_fijo;"
            Dim tabla As New DataTable
            Dim adaptador As New SqlDataAdapter(sql, strCon)
            adaptador.Fill(tabla)

            Dim fila As DataRow                                         ''aqui copio lo que ai en la columna ubicacion
            For Each fila In tabla.Rows                                 ''en el combobox
                ComboBox3.Items.Add(fila.Item("id_af"))
            Next

            Dim strCon2 As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql2 As String = "select nombre_af from activo_fijo;"
            Dim tabla2 As New DataTable
            Dim adaptador2 As New SqlDataAdapter(sql2, strCon2)
            adaptador2.Fill(tabla2)

            Dim fila2 As DataRow                                         ''aqui copio lo que ai en la columna ubicacion
            For Each fila In tabla.Rows                                 ''en el combobox
                ComboBox1.Items.Add(fila.Item("ubicacion"))
            Next


        Catch ex As Exception
            MsgBox("error ", MsgBoxStyle.Critical, "error")
        End Try

    End Sub

    
    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        If RadioButton1.Checked = True Then
            Try
                Dim strCon As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
                Dim sql As String = "select * from activo_fijo where id_af='" & ComboBox3.Text & "';"
                Dim tabla As New DataTable
                Dim adaptador As New SqlDataAdapter(sql, strCon)
                adaptador.Fill(tabla)

                Label29.Text = tabla.Rows(0)("numero_orden")
                Label31.Text = tabla.Rows(0)("nombre_af")
                Label32.Text = tabla.Rows(0)("id_af")
                Label33.Text = tabla.Rows(0)("stock")


            Catch ex As Exception
                MsgBox("Usuario no registrado", MsgBoxStyle.Critical, "Error")

            End Try
        Else
            Try
                Dim strCon As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
                Dim sql As String = "select * from activo_fijo where nombre_af='" & ComboBox1.Text & "';"
                Dim tabla As New DataTable
                Dim adaptador As New SqlDataAdapter(sql, strCon)
                adaptador.Fill(tabla)

                Label29.Text = tabla.Rows(0)("numero_orden")
                Label31.Text = tabla.Rows(0)("nombre_af")
                Label32.Text = tabla.Rows(0)("id_af")
                Label33.Text = tabla.Rows(0)("stock")


            Catch ex As Exception
                MsgBox("Usuario no registrado", MsgBoxStyle.Critical, "Error")

            End Try
        End If
    End Sub
 

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        Try
            Dim strCon As String = "Data Source=(local)\SQLEXPRESS ;Initial Catalog=cegan ;Integrated Security=True"
            Dim sql As String = "insert into mantencion values ('" & TextBox19.Text & "','" & Label32.Text & "','" & DateString & "','" & TextBox21.Text & "','" & TextBox22.Text & "','" & RichTextBox1.Text & "');"
            Dim tabla As New DataTable
            Dim adaptador As New SqlDataAdapter(sql, strCon)
            adaptador.Fill(tabla)

            Me.Button11.PerformClick()

            MsgBox("Datos guardados con exito", MsgBoxStyle.Information, "OK")
        Catch ex As Exception
            MsgBox("Usuario no registrado", MsgBoxStyle.Critical, "Error")

        End Try
    End Sub
End Class